﻿using System;
using System.Web.Mvc;
using Sharp.DataTransferObject.CMS;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts.CMS;
using Sharp.WebBooter.Areas.CMS.Models;

namespace Sharp.WebBooter.Areas.CMS.Controllers
{
    public class SpecialController : Controller
    {
        // GET: Control/Special
        public ActionResult Index()
        {
            return View();
        }
       
        
        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="param"></param>
        /// <param name="propertyId">属性Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetPaged(DataTableParameter param, Guid? propertyId)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var page = proxy.Channel.GetSpecialPaged(param.ToPagingFeature());

                return this.Store(page);
            }
        }
        [Permission(Name = "新增", Order = 1)]
        public ActionResult Create()
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var model = new SpecialViewModel
                {
                    TempFlag = Guid.NewGuid().ToString(),
                    Colmuns = proxy.Channel.GetColumns().Data
                };

                return View("Edit", model);
            }
        }


        [Permission(Name = "编辑", Order = 2)]
        public ActionResult Edit(Guid id)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var model = proxy.Channel.GetSpecialInfo(id).Data.To<SpecialViewModel>();
                model.TempFlag = Guid.NewGuid().ToString();
                model.Colmuns = proxy.Channel.GetColumns().Data;

                return View("Edit", model);
            }
        }


        [HttpPost]
        public ActionResult Save(SpecialDTO dto)
        {
            if (dto == null)
            {
                return this.JsonNet(new Result() { Err = "提交数据出错。" });
            }


            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.SaveSpecial(dto);

                return this.JsonNet(res);
            }
        }

        [HttpPost]
        [Permission(Name = "删除", Order = 3)]
        public ActionResult Delete(Guid[] ids, FormCollection collection)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.DeleteSpecials(ids);
                return this.JsonNet(res);
            }
        }

        public ActionResult Review(Guid id)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var data = proxy.Channel.GetSpecialInfo(id).Data;
                var model = data.To<SpecialReViewViewModel>();

                return View("Review", model);
            }
        }

        [HttpPost]
        public ActionResult SaveReview(ApproveHistory4SpecialDTO vm)
        {
            if (vm == null)
            {
                return this.JsonNet(new Result() { Err = "提交数据出错。" });
            }
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.CheckInSpecial(vm);

                return this.JsonNet(res);
            }
        }
    }

    
}